Source for file SC_GraphPie.php

Documentation is available at SC_GraphPie.php

  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.lockon.co.jp/
  8.  *
  9.  * This program is free software; you can redistribute it and/or
  10.  * modify it under the terms of the GNU General Public License
  11.  * as published by the Free Software Foundation; either version 2
  12.  * of the License, or (at your option) any later version.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  22.  */
  23. $SC_GRAPHPIE_DIR = realpath(dirname( __FILE__));
  24. require_once($SC_GRAPHPIE_DIR . "/SC_GraphBase.php");
  25.  
  26. // 円グラフ生成クラス
  27. class SC_GraphPie extends SC_GraphBase{
  28.     var $cw;
  29.     var $ch;
  30.     var $cz;
  31.     var $cx;
  32.     var $cy;
  33.     var $arrLabel;
  34.     var $arrData;
  35.  
  36.     // コンストラクタ
  37.     function SC_GraphPie($bgw = BG_WIDTH, $bgh = BG_HEIGHT, $left = PIE_LEFT, $top = PIE_TOP) {
  38.         parent::SC_GraphBase($bgw, $bgh, $left, $top);
  39.         // サイズ設定
  40.         $this->setSize(PIE_WIDTH, PIE_HEIGHT, PIE_THICK);
  41.         // 位置設定
  42.         $this->setPosition($this->left + ($this->cw / 2), $this->top + ($this->ch / 2));
  43.     }
  44.  
  45.     // データを360°値に変換する
  46.     function getCircleData($array) {
  47.         $total = "";
  48.         $new_total = "";
  49.         if(!is_array($array)) {
  50.             return;
  51.         }
  52.         $arrRet = array();
  53.         foreach($array as $val) {
  54.             $total += $val;
  55.         }
  56.         if($total <= 0) {
  57.             return;
  58.         }
  59.         $rate = 360 / $total;
  60.         // ラベル表示用
  61.         $p_rate = 100 / $total;
  62.         $cnt = 0;
  63.         foreach($array as $val) {
  64.             $ret = round($val * $rate);
  65.             $new_total+= $ret;
  66.             $arrRet[] = $ret;
  67.             // パーセント表示用
  68.             $this->arrLabel[] = round($val * $p_rate) . " %";
  69.             $cnt++;
  70.         }
  71.         // 合計が360になるように補正しておく
  72.         $arrRet[0] -= $new_total - 360;
  73.         return $arrRet;
  74.     }
  75.  
  76.     // 円の位置設定を行う
  77.     function setPosition($cx, $cy) {
  78.         $this->cx = $cx;
  79.         $this->cy = $cy;
  80.     }
  81.  
  82.     // 円のサイズ設定を行う
  83.     function setSize($cw, $ch, $cz = 0) {
  84.         $this->cw = $cw;
  85.         $this->ch = $ch;
  86.         $this->cz = $cz;
  87.     }
  88.  
  89.     // 影の描画
  90.     function drawShade() {
  91.         $move = 1;
  92.         for($i = ($this->cy + $this->cz); $i <= ($this->cy + $this->cz + ($this->cz * PIE_SHADE_IMPACT)); $i++) {
  93.             imagefilledarc($this->image, $this->cx + $move, $i, $this->cw, $this->ch, 0, 360, $this->shade_color, IMG_ARC_PIE);
  94.             $move += 0.5;
  95.         }
  96.     }
  97.  
  98.     // データをセットする
  99.     function setData($arrData) {
  100.         $this->arrData = array_values($arrData);
  101.     }
  102.  
  103.     // 円グラフを描画する
  104.     function drawGraph() {
  105.         $x = $this->cx;
  106.         $y = $this->cy;
  107.         $z = $this->cz;
  108.         $h = $this->ch;
  109.         $w = $this->cw;
  110.  
  111.         // データの角度を取得する
  112.         $arrRad = $this->getCircleData($this->arrData);
  113.         $rd_max = count($arrRad);
  114.  
  115.         // データが存在しない場合
  116.         if($rd_max <= 0) {
  117.             return;
  118.         }
  119.  
  120.         // 影の描画
  121.         if($this->shade_on) {
  122.             $this->drawShade();
  123.         }
  124.  
  125.         // 色数の取得
  126.         $c_max = count($this->arrColor);
  127.         $dc_max = count($this->arrDarkColor);
  128.  
  129.         // 側面の描画
  130.         for ($i = ($y + $z - 1); $i >= $y; $i--) {
  131.             $start = 0;
  132.             for($j = 0; $j < $rd_max; $j++) {
  133.                 // 角度が0度以上の場合のみ側面を描画する。
  134.                 if($arrRad[$j] > 0) {
  135.                     $end = $start + $arrRad[$j];
  136.                     if($start == 0 && $end == 360) {
  137.                         // -90~270で指定すると円が描画できないので0~360に指定
  138.                         imagearc($this->image, $x, $i, $w, $h, 0, 360, $this->arrDarkColor[($j % $dc_max)]);
  139.                     } else {
  140.                         // -90°は12時の位置から開始するように補正している
  141.                         imagearc($this->image, $x, $i, $w, $h, $start - 90, $end - 90, $this->arrDarkColor[($j % $dc_max)]);
  142.                     }
  143.                     $start = $end;
  144.                 }
  145.             }
  146.         }
  147.         // 底面の描画
  148.         imagearc($this->image, $x, $y + $z, $w, $h, 0, 180 , $this->flame_color);
  149.  
  150.         // 上面の描画
  151.         $start = 0;
  152.         for($i = 0; $i < $rd_max; $i++) {
  153.             $end = $start + $arrRad[$i];
  154.             if($start == 0 && $end == 360) {
  155.                 // -90~270で指定すると円が描画できないので0~360に指定
  156.                 imagefilledarc($this->image, $x, $y, $w, $h, 0, 360, $this->arrColor[($i % $c_max)], IMG_ARC_PIE);
  157.             } else {
  158.                 // -90°は12時の位置から開始するように補正している。
  159.                 imagefilledarc($this->image, $x, $y, $w, $h, $start - 90, $end - 90, $this->arrColor[($i % $c_max)], IMG_ARC_PIE);
  160.             }
  161.             $start = $end;
  162.         }
  163.  
  164.         // 上面の縁取り
  165.         $start = 0;
  166.         for($i = 0; $i < $rd_max; $i++) {
  167.             $end = $start + $arrRad[$i];
  168.             if($start == 0 && $end == 360) {
  169.                 // -90~270で指定すると円が描画できないので0~360に指定
  170.                 imagearc($this->image, $x, $y, $w, $h, 0, 360 , $this->flame_color);
  171.             }
  172.             // -90°は12時の位置から開始するように補正している。
  173.             imagefilledarc($this->image, $x, $y, $w, $h, $start - 90, $end - 90, $this->flame_color, IMG_ARC_EDGED|IMG_ARC_NOFILL);
  174.             $start = $end;
  175.         }
  176.  
  177.         // 側面の縁取り
  178.         imageline($this->image, $x + ($w / 2), $y, $x + ($w / 2), $y + $z, $this->flame_color);
  179.         imageline($this->image, $x - ($w / 2), $y, $x - ($w / 2), $y + $z, $this->flame_color);
  180.         $start = 0;
  181.         for($i = 0; $i < $rd_max; $i++) {
  182.             $end = $start + $arrRad[$i];
  183.             // 前面のみ
  184.             if($end > 90 && $end < 270) {
  185.                 list($ax, $ay) = $this->lfGetArcPos($x, $y, $w, $h, $end);
  186.                 // ラインのずれを補正する
  187.                 if($end > 180) {
  188.                     $ax = $ax + 1;
  189.                 }
  190.                 imageline($this->image, $ax, $ay, $ax, $ay + $z, $this->flame_color);
  191.             }
  192.             $start = $end;
  193.         }
  194.  
  195.         // ラベルの描画
  196.         $this->drawLabel($arrRad);
  197.         // 凡例の描画
  198.         $this->drawLegend(count($this->arrData));
  199.     }
  200.  
  201.     // 円グラフのラベルを描画する
  202.     function drawLabel($arrRad) {
  203.         $rd_max = count($arrRad);
  204.         $start = 0;
  205.         for($i = 0; $i < $rd_max; $i++) {
  206.             $center = $start + ($arrRad[$i] / 2);
  207.             $end = $start + $arrRad[$i];
  208.             list($sx, $sy) = $this->lfGetArcPos($this->cx, $this->cy, ($this->cw / 1.5), ($this->ch / 1.5), $center);
  209.             list($ex, $ey) = $this->lfGetArcPos($this->cx, $this->cy, ($this->cw * 1.5), ($this->ch * 1.5), $center);
  210.             // 指示線の描画
  211.             imageline($this->image, $sx, $sy, $ex + 2, $ey - PIE_LABEL_UP, $this->flame_color);
  212.             $this->setText(FONT_SIZE, $ex - 10, $ey - PIE_LABEL_UP - FONT_SIZE, $this->arrLabel[$i], NULL, 0, true);
  213.             $start = $end;
  214.         }
  215.     }
  216. }
  217. ?>

Documentation generated on Tue, 28 Apr 2009 18:13:23 +0900 by phpDocumentor 1.4.2